<?php

namespace App\Http\Model;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\Hash;
use http\Exception;
class Admin extends Model {
    //
    protected $table = 'admins';
    public $timestamps = false;
    /**
     * 可以被批量赋值的属性。
     * 此处必填,否则不能写入数据库
     *
     * @var array
     */
    protected $fillable = [
        'login_name',
        'true_name',
        'password',
        'c_time'
    ];

    public function getAdminList() {
        $list = $this->paginate(2);
        foreach ($list as $k => $v) {
            $list[$k]['c_time'] = handle_time_f($v['c_time']);
        }
        return $list;
    }

    //插入数据库
    public function getAdminStroe($data) {
        try{
            $adminData['login_name'] = $data['login_name'];
            //判断次姓名是否存在
            $admin_exist = $this->where('login_name', $adminData['login_name'])->exists();
            if ($admin_exist)
                return ['code' => 0, 'msg' => 'this login name is exist'];
            //使用hash加密密码
            $adminData['password'] = Hash::make($data['password']);
            $adminData['phone'] = $data['phone'];
            $adminData['email'] = $data['email'];
            $adminData['c_time'] = ceil(get_millisecond());
            $adminData['like'] = json_encode($data['like']);
            $res = $this->insertGetId($adminData);
        }catch(Exception $e){

        }

        dd($res);
        if ($res) {
            return ['code' => 1, 'msg' => '添加成功'];
        } else {
            return ['code' => 0, 'msg' => '添加失败'];
        }
    }

}
